from django.db import models

# Create your models here.

class Ticket(models.Model):
	STATUS_CHOICES = (	
		(1, 'Open'),
		(2, 'On-Hold'),
		(3, 'Resolved'),
		(4, 'Closed'),
	)

	PRIORITY_CHOICES = (
		(1, 'Project'),
		(2, 'Low'),
		(3, 'Normal'),
		(4, 'High'),
		(5, 'Critical'),
	)

	title = models.CharField(
			verbose_name='ticket title',
			max_length=200
		)
	submitter_email = models.EmailField(
			verbose_name='ticket creator email',
		)
	created = models.DateTimeField(
			verbose_name='ticket creation date',
			auto_now_add=True
		)
	modified = models.DateTimeField(
			verbose_name='last modified date',
			auto_now=True
		)
	status = models.IntegerField(
			verbose_name='ticket status',
			choices=STATUS_CHOICES,
			default=1
		)
	priority = models.IntegerField(
			verbose_name='ticket priority',
			choices=PRIORITY_CHOICES,
			default=3
		)
	description = models.TextField(
			verbose_name='ticket description',
		)
	
	def __unicode__(self):
		return self.title
		
	def email_to_list(self):
		email_list = set([])
		email_list.add(self.submitter_email)
		for note in self.note_set.all():
			email_list.add(note.submitter_email)
		return list(email_list)

	def get_absolute_url(self):
		return('public_ticket_detail', (), {'ticket_id': [str(self.id)]})

	def get_worker_url(self):
		return('worker_portal_ticket', (), {'ticket_id': [str(self.id)]})

	class Meta:
		get_latest_by = 'created'
		ordering = ('status', '-priority')

class Note(models.Model):

	ACTION_CHOICES = (	
		(1, 'User Comment'),
		(2, 'Put Ticket On-Hold'),
		(3, 'Ticket Resolved'),
		(4, 'Ticket Closed'),
		(5, 'Re-Open Ticket'),
	)
	
	ticket = models.ForeignKey(
			Ticket,
			verbose_name='ticket'
		)
	date = models.DateTimeField(
			verbose_name='date',
			auto_now_add=True
		)
	action = models.IntegerField(
			verbose_name='action committed',
			choices=ACTION_CHOICES,
			default=1
		)
	comment = models.TextField(
			verbose_name='comment'
		)
	submitter_email = models.EmailField(
			verbose_name='email address'
		)

	def __unicode__(self):
		return self.comment

	class Meta:
		get_latest_by = 'date'
		ordering = ( 'date', )

